﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Data.SqlClient;

/// <summary>
/// Summary description for Product_Session
/// </summary>
public class Product_Session
{
    public static SqlConnection sqlConnection;
    public static DataTable objDataTable;
	public Product_Session()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    public static List<Product> GetList_Product()
    {

        DataTable dt = DataProvider.GetDataTable("select * from Product");
        List<Product> lst = new List<Product>();
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            Product obj = new Product();

            obj.Pro_ID = (dt.Rows[i]["Pro_ID"] is DBNull) ? int.MinValue : (System.Int32)dt.Rows[i]["Pro_ID"];
            obj.Pro_Name = (dt.Rows[i]["Pro_Name"] is DBNull) ? string.Empty : (System.String)dt.Rows[i]["Pro_Name"];
            obj.Pro_Intro = (dt.Rows[i]["Pro_Intro"] is DBNull) ? string.Empty : (System.String)dt.Rows[i]["Pro_Intro"];
            obj.Pro_Image = (dt.Rows[i]["Pro_Image"] is DBNull) ? string.Empty : (System.String)dt.Rows[i]["Pro_Image"];
            obj.Pro_Status = (dt.Rows[i]["Pro_Status"] is DBNull) ? false : (System.Boolean)dt.Rows[i]["Pro_Status"];
            obj.Pro_TotalView = (dt.Rows[i]["Pro_TotalView"] is DBNull) ? int.MinValue : (System.Int32)dt.Rows[i]["Pro_TotalView"];
            obj.Pro_IsComment = (dt.Rows[i]["Pro_IsComment"] is DBNull) ? false : (System.Boolean)dt.Rows[i]["Pro_IsComment"];
            obj.Pro_Rate = (dt.Rows[i]["Pro_Rate"] is DBNull) ? int.MinValue : (System.Int32)dt.Rows[i]["Pro_Rate"];
            obj.Pro_Content = (dt.Rows[i]["Pro_Content"] is DBNull) ? string.Empty : (System.String)dt.Rows[i]["Pro_Content"];
            obj.Pro_Keyword = (dt.Rows[i]["Pro_Keyword"] is DBNull) ? string.Empty : (System.String)dt.Rows[i]["Pro_Keyword"];
            obj.Pro_BigImage = (dt.Rows[i]["Pro_BigImage"] is DBNull) ? string.Empty : (System.String)dt.Rows[i]["Pro_BigImage"];
            obj.Pro_Price = (dt.Rows[i]["Pro_Price"] is DBNull) ? int.MinValue : (System.Double)dt.Rows[i]["Pro_Price"];
            obj.Cate_ID = (dt.Rows[i]["Cate_ID"] is DBNull) ? int.MinValue : (System.Int32)dt.Rows[i]["Cate_ID"];
            obj.Pro_CreateDate = (dt.Rows[i]["Pro_CreateDate"] is DBNull) ? DateTime.MinValue : (System.DateTime)dt.Rows[i]["Pro_CreateDate"];
            obj.Pro_IsDiscount = (dt.Rows[i]["Pro_IsDiscount"] is DBNull) ? false : (System.Boolean)dt.Rows[i]["Pro_IsDiscount"];
            obj.Pro_PriceDiscount = (dt.Rows[i]["Pro_PriceDiscount"] is DBNull) ? int.MinValue : (System.Double)dt.Rows[i]["Pro_PriceDiscount"];
            obj.Pro_Position = (dt.Rows[i]["Pro_Position"] is DBNull) ? int.MinValue : (System.Int32)dt.Rows[i]["Pro_Position"];

            lst.Add(obj);
        }
        return lst;
    }
    //1, 15, "", "", "Cate_ID=" + ddlCategory.SelectedValue.ToString(), ""
    public static DataTable Get_ProductAsPaging(int pageIndex, int pageSize, string sSortColumn, string sFields, string sWhere, string sGroupBy)
    {
        DataTable dt ;
        dt = DataProvider.GetDataTable("select * from Product  " + sWhere + " " + sFields);
       // WebMsgBox.Show(dt.Rows.Count.ToString());
        return dt;
    }

    public static DataTable Get_ProductAsTable(string whereSql, string orderBy)
    {
        DataTable dt = new DataTable(); // chu y cho nay nek
        dt = DataProvider.GetDataTable("select * from Product where  " + whereSql);
        return dt;
    }
    public static Product Get_Product_ByID(System.Int32 Pro_ID)
    {
        DataTable dt = new DataTable();
        dt = DataProvider.GetDataTable("select * from Product where Pro_ID = " + Pro_ID);
        Product obj = new Product();

        obj.Pro_ID = (dt.Rows[0]["Pro_ID"] is DBNull) ? int.MinValue : (System.Int32)dt.Rows[0]["Pro_ID"];
        obj.Pro_Name = (dt.Rows[0]["Pro_Name"] is DBNull) ? string.Empty : (System.String)dt.Rows[0]["Pro_Name"];
        obj.Pro_Intro = (dt.Rows[0]["Pro_Intro"] is DBNull) ? string.Empty : (System.String)dt.Rows[0]["Pro_Intro"];
        obj.Pro_Image = (dt.Rows[0]["Pro_Image"] is DBNull) ? string.Empty : (System.String)dt.Rows[0]["Pro_Image"];
        obj.Pro_Status = (dt.Rows[0]["Pro_Status"] is DBNull) ? false : (System.Boolean)dt.Rows[0]["Pro_Status"];
        obj.Pro_TotalView = (dt.Rows[0]["Pro_TotalView"] is DBNull) ? int.MinValue : (System.Int32)dt.Rows[0]["Pro_TotalView"];
        obj.Pro_IsComment = (dt.Rows[0]["Pro_IsComment"] is DBNull) ? false : (System.Boolean)dt.Rows[0]["Pro_IsComment"];
        obj.Pro_Rate = (dt.Rows[0]["Pro_Rate"] is DBNull) ? int.MinValue : (System.Int32)dt.Rows[0]["Pro_Rate"];
        obj.Pro_Content = (dt.Rows[0]["Pro_Content"] is DBNull) ? string.Empty : (System.String)dt.Rows[0]["Pro_Content"];
        obj.Pro_Keyword = (dt.Rows[0]["Pro_Keyword"] is DBNull) ? string.Empty : (System.String)dt.Rows[0]["Pro_Keyword"];
        obj.Pro_BigImage = (dt.Rows[0]["Pro_BigImage"] is DBNull) ? string.Empty : (System.String)dt.Rows[0]["Pro_BigImage"];
        obj.Pro_Price = (dt.Rows[0]["Pro_Price"] is DBNull) ? int.MinValue : (System.Double)dt.Rows[0]["Pro_Price"];
        obj.Cate_ID = (dt.Rows[0]["Cate_ID"] is DBNull) ? int.MinValue : (System.Int32)dt.Rows[0]["Cate_ID"];
        obj.Pro_CreateDate = (dt.Rows[0]["Pro_CreateDate"] is DBNull) ? DateTime.MinValue : (System.DateTime)dt.Rows[0]["Pro_CreateDate"];
        obj.Pro_IsDiscount = (dt.Rows[0]["Pro_IsDiscount"] is DBNull) ? false : (System.Boolean)dt.Rows[0]["Pro_IsDiscount"];
        obj.Pro_PriceDiscount = (dt.Rows[0]["Pro_PriceDiscount"] is DBNull) ? int.MinValue : (System.Double)dt.Rows[0]["Pro_PriceDiscount"];
        obj.Pro_Position = (dt.Rows[0]["Pro_Position"] is DBNull) ? int.MinValue : (System.Int32)dt.Rows[0]["Pro_Position"];

        return obj;
    }
    public static void Delete_Product(int Pro_ID)
    {
        try
        {
            String strSQL;
            // xoa comment co pro lien quan
            strSQL = "delete  from Comments where Pro_ID = " + Pro_ID ;
            sqlConnection = new SqlConnection();
            sqlConnection = DataProvider.CreateConnection();
            SqlCommand cmd = new SqlCommand(strSQL, sqlConnection);
            cmd.CommandType = CommandType.Text; ;
            cmd.ExecuteNonQuery();

            // xoa Basket co Pro lien quan
            strSQL = "delete  from Basket where Pro_ID = " + Pro_ID;
            cmd = new SqlCommand(strSQL, sqlConnection);
            cmd.CommandType = CommandType.Text; ;
            cmd.ExecuteNonQuery();

            // xoa pro
            strSQL = "delete  from Product where Pro_ID = " + Pro_ID;
            cmd = new SqlCommand(strSQL, sqlConnection);
            cmd.CommandType = CommandType.Text; ;
            cmd.ExecuteNonQuery();

            sqlConnection.Close();
            cmd = null;
        }
        catch (Exception e) { WebMsgBox.Show("ngoai le produc"); }
        
    }
   


}
